package com.example.atiplat_vodcp;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.apache.commons.lang3.reflect.MethodUtils;

import com.attilax.exception.ExUtil;
import com.attilax.ref.refx;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

public class JsBridge {
	public static String getTrace(Throwable t) {
		StringWriter stringWriter = new StringWriter();
		PrintWriter writer = new PrintWriter(stringWriter);
		t.printStackTrace(writer);
		StringBuffer buffer = stringWriter.getBuffer();
		return buffer.toString();
	}

	MainActivity mainActivity;

	public JsBridge(MainActivity mainActivity2) {
		mainActivity = mainActivity2;
	}
	
	@JavascriptInterface  
	public    String invoke(String s)
	{
		return "test..."+s;
	}
	
	@JavascriptInterface  
	public    String invoke(String s,String s2)
	{
		return "invoke2..."+s;
	}
	
	
	@JavascriptInterface  
	public    String invoke2(  String method,String p1  )
	{
		 List<String> li=new ArrayList();
		 li.add(p1); 
		 Object[] oa=li.toArray();
		return invoke(method,oa);
	}
	
	@JavascriptInterface  
	public    String invoke3(  String method,String p1,String p2 )
	{
		 List<String> li=new ArrayList();
		 li.add(p1);li.add(p2); 
		 Object[] oa=li.toArray();
		return invoke(method,oa);
	}
	
	@JavascriptInterface  
	public    String invoke4(  String method,String p2,String p3,String p4)
	{
		 List<String> li=new ArrayList();
		 li.add(p2);li.add(p3);li.add(p4);
		 Object[] oa=li.toArray();
		return invoke(method,oa);
	}
	// sdk17�汾���ϼ���ע�� solu click btn ma fein ..
	@JavascriptInterface  
	public    String invoke(  String method,   Object... p1) {

		String classname = refx.getClassName(method);
		String meth_name = refx.getMethodName(method);
		Object o;
		boolean flag = true;
		String trace = "$def e";
		try {
			o = ConstructorUtils.invokeConstructor(Class.forName(classname),
					null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// throw new RuntimeException(e);
			flag = false;
			trace = ExUtil.getTrace(e);
			return trace;
		}

		if (flag) {
			try {
				return (String) MethodUtils.invokeMethod(o, meth_name, p1);
			} catch (Exception e) {
				Throwable e2=e;
				if( e instanceof InvocationTargetException)
				{
				// TODO Auto-generated catch block
				  e2=e.getCause();
				
				// throw new RuntimeException(e);
			
				}
				e2.printStackTrace();
				
			 
				trace = ExUtil.getTrace(e2);
				return trace;
			}
		}
		// Handler handler = new Handler();
		// // Callable<V>
		// handler.post(new Runnale(){
		//
		// public void run(){
		//
		// // 更新UI界面元素代码
		//
		// }
		//
		// });
		// handler.
		return trace;

	}

	@JavascriptInterface
	// sdk17�汾���ϼ���ע�� solu click btn ma fein ..
	public void invoke(String method, Object p1, Object p2, Object p3) {

	}

	@JavascriptInterface
	// sdk17�汾���ϼ���ע�� solu click btn ma fein ..
	public void play() {
		try {

			String mv = "smb://192.168.2.106/e/������/��ͷ����Сͷ�ְ�/�´�ͷ���Ӻ�Сͷ�ְ�֮���ܼƻ�.mp4";
			Log.v("::::mv in html ", mv);
			Toast.makeText(mainActivity,
					"play in html,mv:" + new File(mv).exists(),
					Toast.LENGTH_LONG).show();
			String mv2 = Environment.getExternalStorageDirectory().getPath()
					+ "/Test_Movie.m4v";
			mv = "http://127.0.0.1:7788/?file="
					+ URLEncoder.encode(mv, "utf-8");
			// Log.v("URI html:::::::::", uri.toString());
			Uri uri = Uri.parse(mv);
			// ����ϵͳ�Դ��Ĳ�����
			Intent intent = new Intent(Intent.ACTION_VIEW);
			Log.v("URI html:::::::::", uri.toString());
			// intent.setData(uri);
			intent.setDataAndType(uri, "video/*");
			mainActivity.startActivity(intent);
			System.out.println("--form_load finish");

		} catch (Throwable e) {
			Log.i("::::::::::::exp", getTrace(e));
		}
	}

	@JavascriptInterface
	// sdk17�汾���ϼ���ע�� solu click btn ma fein ..
	public void play2() {
		try {

			String mv = "";
			Log.v("::::mv in html ", mv);
			Toast.makeText(mainActivity,
					"play in html,mv:" + new File(mv).exists(),
					Toast.LENGTH_LONG).show();
			String mv2 = Environment.getExternalStorageDirectory().getPath()
					+ "/Test_Movie.m4v";
			mv = "http://127.0.0.1:7777/?smb=" + URLEncoder.encode(mv);

			mv = "http://192.168.2.114/IMG_1084.mp4";
			// Log.v("URI html:::::::::", uri.toString());
			Uri uri = Uri.parse(mv);
			// ����ϵͳ�Դ��Ĳ�����
			Intent intent = new Intent(Intent.ACTION_VIEW);
			Log.v("URI in html:::::::::", uri.toString());
			// intent.setData(uri);
			intent.setDataAndType(uri, "video/*");
			mainActivity.startActivity(intent);
			System.out.println("--play2 _load finish");

		} catch (Throwable e) {
			Log.i("::: exp in player2", getTrace(e));
		}
	}

	@JavascriptInterface
	// sdk17�汾���ϼ���ע�� solu click btn ma fein ..
	public void play3() {
		try {

			String mv = "smb://192.168.2.106/e/������/��ͷ����Сͷ�ְ�/�´�ͷ���Ӻ�Сͷ�ְ�֮���ܼƻ�.mp4";
			Log.v("::::mv in html ", mv);
			Toast.makeText(mainActivity,
					"play in html,mv:" + new File(mv).exists(),
					Toast.LENGTH_LONG).show();
			String mv2 = Environment.getExternalStorageDirectory().getPath()
					+ "/Test_Movie.m4v";
			mv = "http://127.0.0.1:7777/?smb=" + URLEncoder.encode(mv);

			mv = "http://localhost/IMG_1084.mp4";

			String jpg = "smb://192.168.2.106/e/������/��ͷ����Сͷ�ְ�/�´�ͷ���Ӻ�Сͷ�ְ�֮���ܼƻ�.jpg";
			mv = "http://127.0.0.1:7788/?file="
					+ URLEncoder.encode(jpg, "utf-8");
			// Log.v("URI html:::::::::", uri.toString());
			Uri uri = Uri.parse(mv);
			// ����ϵͳ�Դ��Ĳ�����
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			intent.setClassName("com.android.browser",
					"com.android.browser.BrowserActivity");
			Log.v("URI in html:::::::::", uri.toString());
			// intent.setData(uri);
			// intent.setDataAndType(uri, "/*");

			mainActivity.startActivity(intent);
			System.out.println("--play2 _load finish");

		} catch (Throwable e) {
			Log.i("::: exp in player2", getTrace(e));
		}
	}

}
